#! /usr/bin/env python2  
# encoding=utf-8
import sys
reload(sys) 
sys.setdefaultencoding('utf8')
import os
ScriptPath = os.path.split( os.path.realpath( sys.argv[0] ) )[0]
import xmlrpclib
import ConfigParser
import datetime

config = ConfigParser.RawConfigParser()
config.read(ScriptPath+'/sys.cfg')
url = config.get('odoo','host')
uid = 1
pwd = config.get('odoo','password')
db = config.get('odoo','db')
models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))

def getToday():
    date = datetime.datetime.now().strftime('%Y-%m-%d')
    return date

def main():
    print u"开始更新维保证书到期"
    main_certificate = models.execute_kw(db,uid,pwd,'tyibs.base.maintenance.company.employee.certificate','search_read',
        [[['period','<',getToday()],['has_over_time','=',False]]],
        {'fields':['code']}
    )
    con_ids=[]
    for main_c in main_certificate:
        con_ids.append(main_c['id'])
    if len(con_ids) != 0:
        print u"有更新维保证书"
        models.execute_kw(db,uid,pwd,'tyibs.base.maintenance.company.employee.certificate','write',
                        [con_ids,{'has_over_time':True}]
                    )
    else:
        print u"无更新维保证书"
    over2noover()

def over2noover():
    # 反向更新到期证书改为未到期
    print u"开始更新维保证书未到期"
    main_certificate = models.execute_kw(db,uid,pwd,'tyibs.base.maintenance.company.employee.certificate','search_read',
        [[['period','>',getToday()],['has_over_time','=',True]]],
        {'fields':['code']}
    )
    con_ids=[]
    for main_c in main_certificate:
        con_ids.append(main_c['id'])
    if len(con_ids) != 0:
        print u"有更新维保证书"
        models.execute_kw(db,uid,pwd,'tyibs.base.maintenance.company.employee.certificate','write',
                        [con_ids,{'has_over_time':False}]
                    )
    else:
        print u"无更新维保证书"

if __name__ == '__main__':
    main()